package com.schoolcloud.front.controller;

import com.schoolcloud.common.model.NotificationRequest;
import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.R;
import com.schoolcloud.common.util.UserContext;
import com.schoolcloud.front.entity.Notifications;
import com.schoolcloud.front.query.NotificationQuery;
import com.schoolcloud.front.service.NotificationsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/front/notification")
@RequiredArgsConstructor
public class NotificationController {
    private final NotificationsService notificationService;

    @GetMapping("/page")
    public R<PageDTO<Notifications>> getNotificationPage(NotificationQuery  query) {
        return R.ok(notificationService.getNotificationPage(query));
    }
    @PostMapping
    public R<?> saveNotification(@RequestBody Notifications notification) {
        notification.setUserId(UserContext.getUserToInteger());
        return notificationService.save(notification) ? R.ok("保存成功",null) : R.err("保存失败");
    }
    @PutMapping
    public R<?> updateNotification(@RequestBody Notifications notification) {
        return notificationService.updateById(notification) ? R.ok("更新成功",null) : R.err("更新失败");
    }
    @DeleteMapping("/{id}")
    public R<?> deleteNotification(@PathVariable Integer id) {
        return notificationService.removeById(id) ? R.ok("删除成功",null) : R.err("删除失败");
    }
    @GetMapping("/{id}")
    public R<?> getNotification(@PathVariable Integer id) {
        return R.ok(notificationService.getById(id));
    }
}
